package LeetCodeHot100TopInterview;

public class Q152_MaxSubProduct {
    //需要最大累乘与最小累乘 因为可能会负负得正
    //动态规划的思想
    public int maxProduct(int[] nums) {
        if (nums == null || nums.length == 0)
            return 0;

        //前置条件
        int ans = nums[0];
        int max = nums[0];
        int min = nums[0];
        int curMax = 0;
        int curMin = 0;
        for (int i = 1; i < nums.length; i++) {
            //全程需要考虑负负得正
            curMax = Math.max(nums[i], Math.max(max * nums[i], min * nums[i]));
            curMin = Math.min(nums[i], Math.min(max * nums[i], min * nums[i]));
            max = curMax;
            min = curMin;
            ans = Math.max(ans, max);
        }
        return ans;
    }

    public static void main(String[] args) {
        Q152_MaxSubProduct fun = new Q152_MaxSubProduct();
        System.out.println(fun.maxProduct(new int[]{2, 3, -2, 4}));
    }
}
